﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Xman.Dapper.Sqlserver
{
    public static class SqlContextExtensions
    {
        public static string ToSql(this SqlContext context, string sql, object parameters = null)
        {
            if (parameters == null) return sql;

            var paramDict = parameters.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .ToDictionary(p => p.Name, p => p.GetValue(parameters, null));

            // 对于每个参数，尝试将其转换为字符串并安全地嵌入到SQL中
            foreach (var param in paramDict)
            {
                string value = param.Value.ToString();
                sql = sql.Replace("@" + param.Key, value);
            }

            return sql;
        }
    }
}
